﻿/// <reference name="MicrosoftAjax.js"/>
/// <reference name="Doo.Pager.script.js"/>
/// <reference name="Doo.DataSource.base.script.js"/>

Type.registerNamespace("Doo");

Doo.ListView = function (element) {
    this._templatebinder = new Doo.TemplateBinder(this);

    Doo.ListView.initializeBase(this, [element]);
}

Doo.ListView.prototype = {
    initialize: function () {
        Doo.ListView.callBaseMethod(this, 'initialize');

        //加载模板
        if (this.tplurl != null) {
            Doo.TemplateBinder.LoadTpl.load(this.tplurl, this._templatebinder);
        }
    },

    dispose: function () {
        //Add custom dispose actions here
        Doo.ListView.callBaseMethod(this, 'dispose');
    },

    /* 模版 */
    tplurl: null,
    get_tplurl: function () { return this.tplurl; },
    set_tplurl: function (v) {
        if (this.tplurl == v) return;
        this.tplurl = v;
        if (v) Doo.TemplateBinder.LoadTpl.load(v, this._templatebinder);
    },

    set_roottplid: function (id) { this._templatebinder.roottplid = id; },
    get_roottplid: function () { return this._templatebinder.roottplid; },
    set_tpls: function (value) { this._templatebinder.tpls = value; },
    get_tpls: function (value) { return this._templatebinder.tpls; },
    set_tpl: function (name, value) { this._templatebinder.set_tpl(name, value); },

    /* [重载]数据绑定 */
    databind: function (result) {//test
        //判断模板是否读取完成
        var binder = this._templatebinder;
        if (binder.istplready == false) {
            //还在读取中，则继续等待
            var ts = this;
            binder.ontplready = function () { ts.databind(result); };
            return;
        }
        else {//所有条件都已准备好，可以进行绑定操作
            Doo.ListView.callBaseMethod(this, 'databind', [result]);
        }
    },
    _bindtrack: null, //绑定时用
    _items: null, //绑定时用，存储每个item
    get_item: function (i) { return this._items[i]; },
    add_item: function (item) { this._items.push(item); this._currentitem = item; },
    clear_items: function () { this._items = new Array(); },
    _currentitem: null
}

Doo.ListView.registerClass('Doo.ListView', Doo.DataViewBase);

Doo.ListView.ViewItem = function (view) { this.ListView = view; }
Doo.ListView.ViewItem.prototype =
{
    ListView: null,
    _items: new Array(),
    get_item: function (i) { return this._items[i]; },
    add_item: function (item) { this._items.push(item); this.ListView._currentitem = item; },
    clear_items: function () { this._items = new Array(); },

    /* 更新 */
    onupdating: null,
    update: function () {
        var values = {};
        if (this.onupdating) { this.onupdating(this, values); };
        Sys.Debug.trace(values);
    }
}

Doo.ListView.getItem = function (index) {
    var view = this.Items[index];
    if (arguments.length > 1) {
        for (var i = 1; i < arguments.length; i++) {
            view = view.get_item(arguments[i]);
        }
    }
    return view;
}